package sundial;

import java.awt.*;
import javax.swing.*;

/**
 * Class that draws out the gnomon for a sundial.  Uses the calculation module to get access
 * to the latitude for use in drawing the gnomon.
 * 
 * @author Takanori Shiotsu
 * @author Reyn Matsumoto
 *
 */
@SuppressWarnings("serial")
public class Gnomon extends JPanel{

	private int maxHeight = 500;
	private int minHeight = 100;
	
	private double latitude;
	//
	private long convertDouble;
	
    // Create a constructor method
    public Gnomon(SundialCompute calculations){
        super();
        
        //init latitude
        latitude = calculations.getLatitude();
    }

    public void paintComponent(Graphics g){
    	if ((latitude == 0) || (latitude == 90))
    	{
    		//Just needs cylindrical object through inner circle
    	}
    	
    	else
    	{
    		double opposite;

    		opposite = SundialCompute.getOpposite(latitude);

    		if (maxHeight == 500)
    		{
    			convertDouble = Math.round(opposite);

    			if (convertDouble < 500)
    			{
    				//adjacent
    				g.drawLine(100,minHeight,100,maxHeight);

    				//drawing opposite
    				g.drawLine(100,minHeight,(int)convertDouble,minHeight);

    				//drawing hypotenuse
    				g.drawLine((int)convertDouble,minHeight,100,maxHeight);
    				
    				//Label/Instructions
    				g.drawLine(350, 350, 350, 360);//L
    				g.drawLine(350, 360, 360, 360);
    				
    				g.drawLine(365, 350, 365, 360);//E
    				g.drawLine(365, 350, 375, 350);
    				g.drawLine(365, 360, 375, 360);
    				g.drawLine(365, 355, 375, 355);
    				
    				g.drawLine(380, 350, 380, 360);//F
    				g.drawLine(380, 350, 390, 350);
    				g.drawLine(380, 355, 390, 355);
    				
    				g.drawLine(395, 350, 405, 350);//T
    				g.drawLine(400, 350, 400, 360);
    				
    				g.drawLine(407, 350, 409, 350);//:
    				g.drawLine(407, 350, 407, 352);
    				g.drawLine(407, 352, 409, 352);
    				g.drawLine(409, 350, 409, 352);
    				g.drawLine(407, 358, 409, 358);
    				g.drawLine(407, 358, 407, 360);
    				g.drawLine(407, 360, 409, 360);
    				g.drawLine(409, 358, 409, 360);
    				
    				g.drawLine(415, 350, 425, 350);//O
    				g.drawLine(415, 350, 415, 360);
    				g.drawLine(415, 360, 425, 360);
    				g.drawLine(425, 350, 425, 360);
    				
    				g.drawLine(430, 350, 430, 360);//N
    				g.drawLine(430, 350, 440, 360);
    				g.drawLine(440, 350, 440, 360);
    				
    				g.drawLine(455, 350, 465, 350);//S
    				g.drawLine(455, 350, 455, 355);
    				g.drawLine(455, 355, 465, 355);
    				g.drawLine(465, 355, 465, 360);
    				g.drawLine(455, 360, 465, 360);
    				
    				g.drawLine(470, 350, 470, 360);//U
    				g.drawLine(470, 360, 480, 360);
    				g.drawLine(480, 360, 480, 350);
    				
    				g.drawLine(485, 350, 485, 360);//N
    				g.drawLine(485, 350, 495, 360);
    				g.drawLine(495, 360, 495, 350);
    				
    				g.drawLine(500, 350, 500, 360);//D
    				g.drawLine(500, 350, 505, 353);
    				g.drawLine(505, 353, 505, 357);
    				g.drawLine(505, 357, 500, 360);
    				
    				g.drawLine(510, 350, 520, 350);//I
    				g.drawLine(515, 350, 515, 360);
    				g.drawLine(510, 360, 520, 360);
    				
    				g.drawLine(525, 350, 525, 360);//A
    				g.drawLine(525, 350, 535, 350);
    				g.drawLine(535, 350, 535, 360);
    				g.drawLine(525, 355, 535, 355);
    				
    				g.drawLine(540, 350, 540, 360);//L
    				g.drawLine(540, 360, 550, 360);
    				
    				////
    				g.drawLine(350, 365, 350, 375);//E
    				g.drawLine(350, 365, 360, 365);
    				g.drawLine(350, 375, 360, 375);
    				g.drawLine(350, 370, 360, 370);
    				
    				g.drawLine(365, 365, 365, 375);//D
    				g.drawLine(365, 365, 370, 368);
    				g.drawLine(370, 368, 370, 372);
    				g.drawLine(370, 372, 365, 375);
    				
    				g.drawLine(375, 365, 385, 365);//G
    				g.drawLine(375, 365, 375, 375);
    				g.drawLine(375, 375, 385, 375);
    				g.drawLine(385, 375, 385, 370);
    				g.drawLine(385, 370, 380, 370);
    				
    				g.drawLine(390, 365, 400, 365);//E
    				g.drawLine(390, 365, 390, 375);
    				g.drawLine(390, 370, 400, 370);
    				g.drawLine(390, 375, 400, 375);
    			}
    			else
    			{
    				//will result in couple of pixels off, but will fix too large problem
    				while (convertDouble > 500)
    				{
    					if (convertDouble % 2 == 1)
    					{
    						convertDouble /= 3;
    						maxHeight /= 3;
    					}
    					else
    					{
    						convertDouble /= 2;
    						maxHeight /= 2;
    					}
    				}

    				//adjacent
    				g.drawLine(100,minHeight,100,maxHeight);

    				//drawing opposite
    				g.drawLine(100,minHeight,(int)convertDouble,minHeight);

    				//drawing hypotenuse
    				g.drawLine((int)convertDouble,minHeight,100,maxHeight);
    				
    				
    				//Label/Instructions
    				g.drawLine(350, 350, 350, 360);//L
    				g.drawLine(350, 360, 360, 360);
    				
    				g.drawLine(365, 350, 365, 360);//E
    				g.drawLine(365, 350, 375, 350);
    				g.drawLine(365, 360, 375, 360);
    				g.drawLine(365, 355, 375, 355);
    				
    				g.drawLine(380, 350, 380, 360);//F
    				g.drawLine(380, 350, 390, 350);
    				g.drawLine(380, 355, 390, 355);
    				
    				g.drawLine(395, 350, 405, 350);//T
    				g.drawLine(400, 350, 400, 360);
    				
    				g.drawLine(407, 350, 409, 350);//:
    				g.drawLine(407, 350, 407, 352);
    				g.drawLine(407, 352, 409, 352);
    				g.drawLine(409, 350, 409, 352);
    				g.drawLine(407, 358, 409, 358);
    				g.drawLine(407, 358, 407, 360);
    				g.drawLine(407, 360, 409, 360);
    				g.drawLine(409, 358, 409, 360);
    				
    				g.drawLine(415, 350, 425, 350);//O
    				g.drawLine(415, 350, 415, 360);
    				g.drawLine(415, 360, 425, 360);
    				g.drawLine(425, 350, 425, 360);
    				
    				g.drawLine(430, 350, 430, 360);//N
    				g.drawLine(430, 350, 440, 360);
    				g.drawLine(440, 350, 440, 360);
    				
    				g.drawLine(455, 350, 465, 350);//S
    				g.drawLine(455, 350, 455, 355);
    				g.drawLine(455, 355, 465, 355);
    				g.drawLine(465, 355, 465, 360);
    				g.drawLine(455, 360, 465, 360);
    				
    				g.drawLine(470, 350, 470, 360);//U
    				g.drawLine(470, 360, 480, 360);
    				g.drawLine(480, 360, 480, 350);
    				
    				g.drawLine(485, 350, 485, 360);//N
    				g.drawLine(485, 350, 495, 360);
    				g.drawLine(495, 360, 495, 350);
    				
    				g.drawLine(500, 350, 500, 360);//D
    				g.drawLine(500, 350, 505, 353);
    				g.drawLine(505, 353, 505, 357);
    				g.drawLine(505, 357, 500, 360);
    				
    				g.drawLine(510, 350, 520, 350);//I
    				g.drawLine(515, 350, 515, 360);
    				g.drawLine(510, 360, 520, 360);
    				
    				g.drawLine(525, 350, 525, 360);//A
    				g.drawLine(525, 350, 535, 350);
    				g.drawLine(535, 350, 535, 360);
    				g.drawLine(525, 355, 535, 355);
    				
    				g.drawLine(540, 350, 540, 360);//L
    				g.drawLine(540, 360, 550, 360);
    				
    				////
    				g.drawLine(350, 365, 350, 375);//E
    				g.drawLine(350, 365, 360, 365);
    				g.drawLine(350, 375, 360, 375);
    				g.drawLine(350, 370, 360, 370);
    				
    				g.drawLine(365, 365, 365, 375);//D
    				g.drawLine(365, 365, 370, 368);
    				g.drawLine(370, 368, 370, 372);
    				g.drawLine(370, 372, 365, 375);
    				
    				g.drawLine(375, 365, 385, 365);//G
    				g.drawLine(375, 365, 375, 375);
    				g.drawLine(375, 375, 385, 375);
    				g.drawLine(385, 375, 385, 370);
    				g.drawLine(385, 370, 380, 370);
    				
    				g.drawLine(390, 365, 400, 365);//E
    				g.drawLine(390, 365, 390, 375);
    				g.drawLine(390, 370, 400, 370);
    				g.drawLine(390, 375, 400, 375);
    			}
    		}
    		
    		else
    		{
    			//adjacent
				g.drawLine(100,minHeight,100,maxHeight);

				//drawing opposite
				g.drawLine(100,minHeight,(int)convertDouble,minHeight);

				//drawing hypotenuse
				g.drawLine((int)convertDouble,minHeight,100,maxHeight);
				
				//Label/Instructions
				g.drawLine(350, 350, 350, 360);//L
				g.drawLine(350, 360, 360, 360);
				
				g.drawLine(365, 350, 365, 360);//E
				g.drawLine(365, 350, 375, 350);
				g.drawLine(365, 360, 375, 360);
				g.drawLine(365, 355, 375, 355);
				
				g.drawLine(380, 350, 380, 360);//F
				g.drawLine(380, 350, 390, 350);
				g.drawLine(380, 355, 390, 355);
				
				g.drawLine(395, 350, 405, 350);//T
				g.drawLine(400, 350, 400, 360);
				
				g.drawLine(407, 350, 409, 350);//:
				g.drawLine(407, 350, 407, 352);
				g.drawLine(407, 352, 409, 352);
				g.drawLine(409, 350, 409, 352);
				g.drawLine(407, 358, 409, 358);
				g.drawLine(407, 358, 407, 360);
				g.drawLine(407, 360, 409, 360);
				g.drawLine(409, 358, 409, 360);
				
				g.drawLine(415, 350, 425, 350);//O
				g.drawLine(415, 350, 415, 360);
				g.drawLine(415, 360, 425, 360);
				g.drawLine(425, 350, 425, 360);
				
				g.drawLine(430, 350, 430, 360);//N
				g.drawLine(430, 350, 440, 360);
				g.drawLine(440, 350, 440, 360);
				
				g.drawLine(455, 350, 465, 350);//S
				g.drawLine(455, 350, 455, 355);
				g.drawLine(455, 355, 465, 355);
				g.drawLine(465, 355, 465, 360);
				g.drawLine(455, 360, 465, 360);
				
				g.drawLine(470, 350, 470, 360);//U
				g.drawLine(470, 360, 480, 360);
				g.drawLine(480, 360, 480, 350);
				
				g.drawLine(485, 350, 485, 360);//N
				g.drawLine(485, 350, 495, 360);
				g.drawLine(495, 360, 495, 350);
				
				g.drawLine(500, 350, 500, 360);//D
				g.drawLine(500, 350, 505, 353);
				g.drawLine(505, 353, 505, 357);
				g.drawLine(505, 357, 500, 360);
				
				g.drawLine(510, 350, 520, 350);//I
				g.drawLine(515, 350, 515, 360);
				g.drawLine(510, 360, 520, 360);
				
				g.drawLine(525, 350, 525, 360);//A
				g.drawLine(525, 350, 535, 350);
				g.drawLine(535, 350, 535, 360);
				g.drawLine(525, 355, 535, 355);
				
				g.drawLine(540, 350, 540, 360);//L
				g.drawLine(540, 360, 550, 360);
				
				////
				g.drawLine(350, 365, 350, 375);//E
				g.drawLine(350, 365, 360, 365);
				g.drawLine(350, 375, 360, 375);
				g.drawLine(350, 370, 360, 370);
				
				g.drawLine(365, 365, 365, 375);//D
				g.drawLine(365, 365, 370, 368);
				g.drawLine(370, 368, 370, 372);
				g.drawLine(370, 372, 365, 375);
				
				g.drawLine(375, 365, 385, 365);//G
				g.drawLine(375, 365, 375, 375);
				g.drawLine(375, 375, 385, 375);
				g.drawLine(385, 375, 385, 370);
				g.drawLine(385, 370, 380, 370);
				
				g.drawLine(390, 365, 400, 365);//E
				g.drawLine(390, 365, 390, 375);
				g.drawLine(390, 370, 400, 370);
				g.drawLine(390, 375, 400, 375);
    		}
    	}
    }
    
    
    public static JFrame makeGnomon(SundialCompute calculations)
    {
    	JFrame newFrame = new JFrame("Gnomon");
    	newFrame.setSize(600,600);
    	
    	Gnomon gnomon = new Gnomon(calculations);
    	newFrame.setContentPane(gnomon);
    	
    	return newFrame;
    }
    
}